package br.com.lh.urabuilder.util.pointchooser
{
	import br.com.lh.urabuilder.util.CoordinatesUtils;
	import br.com.lh.urabuilder.view.ActionComponent;
	/**
	 * From two given UraActionComponentElements locate the best two points of them 
	 * that will be used for building the ArrowContainer element.
	 * 
	 */
	public class TwoComponentPointChooser implements IPointChooser
	{
		private var startComponent:ActionComponent;
		private var endComponent:ActionComponent;
		private var _pointArray:Array;
		
		public function TwoComponentPointChooser(startComponent:ActionComponent,endComponent:ActionComponent){
			this.startComponent = startComponent;
			this.endComponent = endComponent;
			this._pointArray = pointArray;
		}
		
		public function get pointArray():Array{
			var isBelow:Boolean=isValueGreater(endComponent.y, startComponent.y);
			var isRight:Boolean = isValueGreater(endComponent.x,startComponent.x);
			var ratio:Number = getRatio(endComponent,startComponent);
			if (Math.abs(ratio) > 1){	
				return yGreater(isBelow,isRight);
			}else{
				return xGreater(isBelow,isRight);
			}
		} 
		private function yGreater(isBelow:Boolean,isRight:Boolean):Array{
			if (isBelow){
				return [startComponent.getBottomPoint(),endComponent.getTopPoint()];
			}else{
				return [startComponent.getTopPoint(),endComponent.getBottomPoint()];
			}
			
		}
		private function xGreater(isBelow:Boolean,isRight:Boolean):Array{
			if (isRight){
				return [startComponent.getRightPoint(),endComponent.getLeftPoint()];
			}else{
				return [startComponent.getLeftPoint(),endComponent.getRightPoint()];
			}
		}
		
		private function isValueGreater(endValue:Number,startValue:Number):Boolean{
			return endValue >= startValue;
		}
		
		public function getRatio(endComponent:ActionComponent,startComponent:ActionComponent):Number{
			var yDelta:Number = endComponent.y - startComponent.y;
			var xDelta:Number = endComponent.x - startComponent.x;
			return yDelta/xDelta;
		}
	}
}